package com.olympia.myapp.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.olympia.myapp.R;
import com.olympia.myapp.server.URLServer;
import com.olympia.myapp.util.MessageUtil;
import com.olympia.myapp.util.StringUtil;

public class BaseActivity extends Activity {
	protected Animation animShow, animHide; // tao hieu ung de an hien popUp
	public DisplayMetrics dm;
	protected Intent intent;
	protected URLServer urlUtil;
	protected MessageUtil msgUtil = null;
	protected StringUtil strUtil = null;
	protected boolean isRequest = false;
	protected boolean isBack = false;

	public boolean isFullscreen = true;
	public Context context = null;

	// Display
	public int SCREEN_WIDTH = 0;
	public int SCREEN_HEIGHT = 0;
	public float SCREEN_XDPI = 0;
	public float SCREEN_YDPI = 0;
	public float DENSITY = 0;
	protected String LOGTAG = "Error";

	protected void onCreate(Bundle savedInstanceState) {
		this.dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(this.dm);

		this.SCREEN_HEIGHT = this.dm.heightPixels;
		this.SCREEN_WIDTH = this.dm.widthPixels;

		this.SCREEN_XDPI = this.dm.xdpi;
		this.SCREEN_YDPI = this.dm.ydpi;

		this.DENSITY = this.dm.density;

		this.context = this;
		this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
		super.onCreate(savedInstanceState);
		this.urlUtil = new URLServer();
		this.msgUtil = new MessageUtil();
		this.strUtil = new StringUtil();

		// khoi tao animation
		this.animShow = AnimationUtils.loadAnimation(this,
				R.anim.anim_show_down);
		this.animHide = AnimationUtils.loadAnimation(this,
				R.anim.anim_hide_down);
	}

	/**
	 * You can set the application to fullscreen or to normal screen with the
	 * method
	 * 
	 * @param fullscreen
	 *            true if it should go to fullscreen
	 */
	public void setFullscreen(boolean fullscreen) {
		if (fullscreen) {
			this.getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_FULLSCREEN);
			this.getWindow().clearFlags(
					WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
			this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		} else {
			this.getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
			this.getWindow().clearFlags(
					WindowManager.LayoutParams.FLAG_FULLSCREEN);
			this.getWindow().requestFeature(Window.FEATURE_CUSTOM_TITLE);
		}
		this.isFullscreen = fullscreen;
	}

	/**
	 * You can use this as the CandroidSurfaceView parameter
	 * 
	 * @return returns the screen specified DisplayMetrics
	 */
	public DisplayMetrics getDisplayMetrics() {
		return dm;
	}

	public boolean onTouchEvent(MotionEvent event) {
		int x = (int) event.getX();
		int y = (int) event.getY();
		int pressure = (int) (event.getPressure() * 100);

		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			onTouchDown(x, y, pressure);
			return true;
		} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
			onTouchMove(x, y, pressure);
		} else if (event.getAction() == MotionEvent.ACTION_UP) {
			onTouchUp(x, y, pressure);
			return super.onTouchEvent(event);
		}
		return super.onTouchEvent(event);
	}

	/**
	 * onTouchDown get called when the user touches the screen.
	 * 
	 * @param touchX
	 *            The x coordinate of the finger
	 * @param touchY
	 *            The y coordinate of the finger
	 * @param pressure
	 *            The size of the finger
	 */
	public void onTouchDown(int touchX, int touchY, int pressure) {
	}

	/**
	 * 
	 * onTouchMove get called when the user moves the finger on the screen.
	 * 
	 * @param touchX
	 *            The x coordinate of the finger
	 * @param touchY
	 *            The y coordinate of the finger
	 * @param pressure
	 *            The size of the finger
	 */
	public void onTouchMove(int touchX, int touchY, int pressure) {
	}

	/**
	 * onTouchUp get called when the user releases the screen with the finger.
	 * 
	 * @param touchX
	 *            The x coordinate of the finger
	 * @param touchY
	 *            The y coordinate of the finger
	 * @param pressure
	 *            The size of the finger
	 */
	public void onTouchUp(int touchX, int touchY, int pressure) {
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {

		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_UP:
			onDPadUp();
			onTrackballUp();
			break;
		case KeyEvent.KEYCODE_DPAD_DOWN:
			onDPadDown();
			onTrackballDown();
			break;
		case KeyEvent.KEYCODE_DPAD_LEFT:
			onDPadLeft();
			onTrackballLeft();
			break;
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			onDPadRight();
			onTrackballRight();
			break;
		case KeyEvent.KEYCODE_DPAD_CENTER:
			onDPadCenter();
			onTrackballPress();
			break;
		case KeyEvent.KEYCODE_HOME:
			onHomeButton();
			break;
		case KeyEvent.KEYCODE_BACK:
			onBackButton();
			break;
		case KeyEvent.KEYCODE_SEARCH:
			onSearchButton();
			break;
		case KeyEvent.KEYCODE_MENU:
			onMenuButton();
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

	public boolean onTrackballEvent(MotionEvent event) {
		return super.onTrackballEvent(event);
	}

	/**
	 * onTrackballLeft get called when the user moves the trackball left.
	 */
	public void onTrackballLeft() {
	}

	/**
	 * onTrackballRight get called when the user moves the trackball right.
	 */
	public void onTrackballRight() {
	}

	/**
	 * onTrackballUp get called when the user moves the trackball up.
	 */
	public void onTrackballUp() {
	}

	/**
	 * onTrackballDown get called when the user moves the trackball down.
	 */
	public void onTrackballDown() {
	}

	/**
	 * onTrackballPress get called when the user presses the trackball.
	 */
	public void onTrackballPress() {
	}

	/**
	 * onDPadLeft get called when the user press the left button on the D-Pad
	 */
	public void onDPadLeft() {
	}

	/**
	 * onDPadRight get called when the user press the right button on the D-Pad
	 */
	public void onDPadRight() {
	}

	/**
	 * onDPadUp get called when the user press the up button on the D-Pad
	 */
	public void onDPadUp() {
	}

	/**
	 * onDPadDown get called when the user press the down button on the D-Pad
	 */
	public void onDPadDown() {
	}

	/**
	 * onDPadCenter get called when the user press the center button on the
	 * D-Pad
	 */
	public void onDPadCenter() {
	}

	/**
	 * onHomeButton get called when the user presses the Home button
	 */
	public void onHomeButton() {
	}

	/**
	 * onBackButton get called when the user presses the Back button
	 */
	public void onBackButton() {
	}

	/**
	 * onSearchButton get called when the user presses the Search button
	 */
	public void onSearchButton() {
	}

	/**
	 * onMenuButton get called when the user presses the Menu button
	 */
	public void onMenuButton() {
	}

	protected void onDestroy() {
		try {
			finalize();
		} catch (Throwable e) {
		}
		super.onDestroy();
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		View v = getCurrentFocus();
		boolean ret = super.dispatchTouchEvent(event);

		if (v instanceof EditText) {
			View w = getCurrentFocus();
			int scrcoords[] = new int[2];
			w.getLocationOnScreen(scrcoords);
			float x = event.getRawX() + w.getLeft() - scrcoords[0];
			float y = event.getRawY() + w.getTop() - scrcoords[1];

			if (event.getAction() == MotionEvent.ACTION_UP
					&& (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w
							.getBottom())) {
				InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(getWindow().getCurrentFocus()
						.getWindowToken(), 0);
			}
		}
		return ret;
	}

}
